<?php
require_once($application_folder."/controllers/navigator.php");
class Login extends navigator 
{

    function __construct()
    {
        parent::__construct(); 
        $this->layout->setLayout('layout_login');
        $this->load->helper('date');
    }

    function index(){ 
        $this->loginform();
    }    

    function loginform(){

        if($this->session->userdata('logged_in')){
            redirect(base_url().'article/preview');   
        }else{
            $this->core_cms('login', '', 'login');   
        }        

    }

    function loginuser(){

        if($this->validloginform()) {

            if($this->simple_login_secure->login($_POST['email'], $_POST['password'])){

                echo json_encode(array('action' => true)); 

            }else{

                echo json_encode(array('action' => false, 'msg' => '<p>Molimo unesite ispravne pristupne podatke!</p>')); 
            }
        }
        else{

            echo json_encode(array('action' => false, 'msg' => validation_errors()));

        }

    }

    function logout(){

        if($this->session->userdata('logged_in')){ 

            $this->simple_login_secure->logout();
            //$this->userlog->addaction('LOGOUT');
            redirect(base_url());

        }else{
            $this->core_cms('login', '', 'login');   
        }
    }

    function validloginform() {

        $this->form_validation->set_rules('email','<strong>email</strong>','trim|xss_clean|required|valid_email');        
        $this->form_validation->set_rules('password','<strong>lozinku</strong>','trim|xss_clean|required');

        $this->form_validation->set_message('required','Molimo unesite %s!'); 
        $this->form_validation->set_message('valid_email','Polje %s mora biti validan mejl!'); 

        return $this->form_validation->run();

    }

    

}